﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Submarines
{
    public partial class SeaBattleControl : UserControl
    {
        frmTemp _mainForm;
        Button[,] myTable = new Button[10, 10];
        object _tag_for_button; // 0=Clear / 1=Ship / 2=Near / 3=Hit / 4=Missed
        Button _tempButton;
        Image _img;
        public int _hited = 0;
        enum Status { Clear = 0, Ship = 1, Near = 2, Hit = 3, Missed = 4 }
        //Gai
        int[] _Count = new int[4];


        public SeaBattleControl()
        {
            InitializeComponent();
        }

        private void SeaBattleControl_Load(object sender, EventArgs e)
        {
            FillMyTable();
            _tag_for_button = 0;
        }

        private void FillMyTable()
        {
            _tag_for_button = 0;
            int x = 0, y = 0;
            for (y = 0; y < 10; y++)
                for (x = 0; x < 10; x++)
                {
                    _tempButton = new Button();
                    _tempButton.Name = "btn" + x.ToString() + "_" + y.ToString();
                    _tempButton.Tag = _tag_for_button;
                    _tempButton.Dock = DockStyle.Fill;
                    _tempButton.FlatStyle = FlatStyle.Flat;
                    _tempButton.Margin = new Padding(0);
                    _tempButton.BackgroundImage = global::Submarines.Properties.Resources.water_resize;
                    _tempButton.BackgroundImageLayout = ImageLayout.Stretch;
                    //_tempButton.Text = x.ToString() + "_" + y.ToString();
                    _tempButton.AllowDrop = true;
                    _tempButton.DragOver += new DragEventHandler(tempButton_DragOver);
                    _tempButton.DragDrop += new DragEventHandler(tempButton_DragDrop);
                    _tempButton.Click += new EventHandler(tempButton_Click);
                    //myTable[x, y] = _tempButton;
                    //Gai
                    _tempButton.MouseClick += new MouseEventHandler(Board_MouseClick);
                    _tempButton.DragEnter += new DragEventHandler(Board_DragEnter);
                    _tempButton.DragLeave += new EventHandler(Board_DragLeave);
                    _tempButton.DragDrop += new DragEventHandler(Board_DragDrop);

                    tblPanel_My.Controls.Add(_tempButton, x, y);
                }
            //tblPanel_My.BackgroundImage = global::SeaBatle.Properties.Resources.J_water_1_;
        }

        void tempButton_DragDrop(object sender, DragEventArgs e)
        {
            Button target = (Button)sender;
            Button source = (Button)e.Data.GetData(typeof(Button));
            AddShipp(target, source,true);
            Global._added_ok = true;
        }

        public bool AddRemoteShip(Button source, int column, int row, bool image)
        {
            Button target = (Button)tblPanel_My.GetControlFromPosition(column, row);
            AddShipp(target, source, image);
            return true;
        }

        void AddShipp(Button target, Button source, bool with_image)
        {
            int lenght = int.Parse(source.Name.Substring(4,1));
            bool horisntal = source.Name.Substring(5, 1).Equals("H");
            TableLayoutPanelCellPosition tblpcp = tblPanel_My.GetCellPosition(target);
                if (horisntal)
                {
                    for (int i = 0; i < lenght; i++)
                    {
                        //GetImage(i, lenght, horisntal);
                       // tblPanel_My.GetControlFromPosition(i + tblpcp.Column, tblpcp.Row).BackColor = Color.Black;
                        if (with_image)
                            tblPanel_My.GetControlFromPosition(i + tblpcp.Column, tblpcp.Row).BackgroundImage = GetImage(i + 1, lenght, horisntal);
                        tblPanel_My.GetControlFromPosition(i + tblpcp.Column, tblpcp.Row).Tag = 1;
                        MarkCellsAroundShip(tblpcp.Column+i, tblpcp.Row);
                    }
                }
                else
                {
                    for (int i = 0; i < lenght; i++)
                    {
                        //tblPanel_My.GetControlFromPosition(tblpcp.Column, tblpcp.Row + i).BackColor = Color.Black;
                        if (with_image)
                            tblPanel_My.GetControlFromPosition(tblpcp.Column, tblpcp.Row + i).BackgroundImage = GetImage(i + 1, lenght, horisntal);
                        tblPanel_My.GetControlFromPosition(tblpcp.Column, tblpcp.Row + i).Tag = 1;
                        MarkCellsAroundShip(tblpcp.Column, tblpcp.Row+i);
                    }
                }
        }

        private Image GetImage(int i, int lenght, bool horisntal)
        {
            if (horisntal)
                switch (lenght)
                {
                    case 1:
                        _img = global::Submarines.Properties.Resources.ship_1;
                        break;
                    case 2:
                        switch (i)
                        {
                            case 1:
                                _img = global::Submarines.Properties.Resources.ship_F_H;
                                break;
                            case 2:
                                _img = global::Submarines.Properties.Resources.ship_B_H;
                                break;
                        }
                        break;

                    case 3:
                        switch (i)
                        {
                            case 1:
                                _img = global::Submarines.Properties.Resources.ship_F_H;
                                break;
                            case 2:
                                _img = global::Submarines.Properties.Resources.ship_M_H;
                                break;
                            case 3:
                                _img = global::Submarines.Properties.Resources.ship_B_H;
                                break;
                        }
                        break;
                    case 4:
                        switch (i)
                        {
                            case 1:
                                _img = global::Submarines.Properties.Resources.ship_F_H;
                                break;
                            case 2:
                                _img = global::Submarines.Properties.Resources.ship_M_H;
                                break;
                            case 3:
                                _img = global::Submarines.Properties.Resources.ship_M_H;
                                break;
                            case 4:
                                _img = global::Submarines.Properties.Resources.ship_B_H;
                                break;
                        }
                        break;

                    default:
                        _img = null;
                        break;
                }
            else
            {
                switch (lenght)
                {
                    case 1:
                        _img = global::Submarines.Properties.Resources.ship_1;
                        break;
                    case 2:
                        switch (i)
                        {
                            case 1:
                                _img = global::Submarines.Properties.Resources.ship_F_V;
                                break;
                            case 2:
                                _img = global::Submarines.Properties.Resources.ship_B_V;
                                break;
                        }
                        break;

                    case 3:
                        switch (i)
                        {
                            case 1:
                                _img = global::Submarines.Properties.Resources.ship_F_V;
                                break;
                            case 2:
                                _img = global::Submarines.Properties.Resources.ship_M_V;
                                break;
                            case 3:
                                _img = global::Submarines.Properties.Resources.ship_B_V;
                                break;
                        }
                        break;
                    case 4:
                        switch (i)
                        {
                            case 1:
                                _img = global::Submarines.Properties.Resources.ship_F_V;
                                break;
                            case 2:
                                _img = global::Submarines.Properties.Resources.ship_M_V;
                                break;
                            case 3:
                                _img = global::Submarines.Properties.Resources.ship_M_V;
                                break;
                            case 4:
                                _img = global::Submarines.Properties.Resources.ship_B_V;
                                break;
                        }
                        break;

                    default:
                        _img = null;
                        break;
                }

            }


            return _img;
        }

        void MarkCellsAroundShip(int col, int row)
        {
            for (int x=-1; x<=1; x++)
                for (int y = -1; y <= 1; y++)
                {
                    try
                    {
                        Button btnTem = (Button)tblPanel_My.GetControlFromPosition(col + x, row + y);

                        if (int.Parse(btnTem.Tag.ToString()) == 0)
                        {
                            btnTem.Tag = 2;
                        }
                    }
                    catch (Exception) { }
                }
        }
        public void ShowMissedAround(int col, int row)
        {
            Button btnTem;
            try 
            {
                 btnTem = (Button)tblPanel_My.GetControlFromPosition(col -1, row -1);
                btnTem.Image = global::Submarines.Properties.Resources.missed;
                 btnTem = (Button)tblPanel_My.GetControlFromPosition(col - 1, row + 1);
                btnTem.Image = global::Submarines.Properties.Resources.missed;
                 btnTem = (Button)tblPanel_My.GetControlFromPosition(col + 1, row - 1);
                btnTem.Image = global::Submarines.Properties.Resources.missed;
                 btnTem = (Button)tblPanel_My.GetControlFromPosition(col + 1, row + 1);
                btnTem.Image = global::Submarines.Properties.Resources.missed;
            }
            catch (Exception) { }
        }
        public bool DropOk(TableLayoutPanelCellPosition tblCellPosition, int lenght, bool hor)
        {
            bool result = true;
            int i;
            if (hor)
            {
                for (i = 0; i < lenght; i++)
                {
                    if (!CheckSellStatusOk(tblCellPosition.Column + i, tblCellPosition.Row))
                        result = false;
                }
            }
            else
            {
                for (i = 0; i < lenght; i++)
                {
                    if (!CheckSellStatusOk(tblCellPosition.Column, tblCellPosition.Row + i))
                        result = false;
                }
            }
            return result;
        }

        bool CheckSellStatusOk(int col, int row)
        {
            Button btnTem = (Button)tblPanel_My.GetControlFromPosition(col, row);
            if (btnTem == null)
                return false;
            int cell_status = int.Parse(btnTem.Tag.ToString());
            if (cell_status == 0 )
                return true;
            else
                return false;
        }

        void tempButton_DragOver(object sender, DragEventArgs e)
        {
            Button target = (Button)sender;
            Button source = (Button)e.Data.GetData(typeof(Button));
            int lenght = int.Parse(source.Name.Substring(4, 1));
            bool horisntal = source.Name.Substring(5, 1).Equals("H");
            TableLayoutPanelCellPosition tblpcp = tblPanel_My.GetCellPosition(target);
            if (!DropOk(tblpcp, lenght, horisntal))
            {
                e.Effect = DragDropEffects.None;
            }
            else
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        public void tempButton_Click(object sender, EventArgs e)
        {
            if (Global._user_turn)
            {
                Button b = sender as Button;
                Hit(b);
                if (_hited == 20)
                {
                    MessageBox.Show("You WIN !!!");
                }
            }
        }

        public bool Hit(Button btn)
        {
            _mainForm = new frmTemp();
            bool hitted_ok = false;
            switch (btn.Tag.ToString())
            {
                case "0":
                    btn.BackgroundImage = global::Submarines.Properties.Resources.missed;
                    btn.Tag = "4";
                    if (Global._user_turn)
                        Global._user_turn = false;
                    else
                        Global._user_turn = true;
                    break;
                case "1":
                    btn.BackgroundImage = global::Submarines.Properties.Resources.exp1;
                    btn.Tag = "3";
                    hitted_ok = true;
                    _hited++;
                    lblHitsLeft.Text = (20 - _hited).ToString();
                    //MessageBox.Show("HIT:" + _hited);
                    break;
                case "2":
                    btn.BackgroundImage = global::Submarines.Properties.Resources.missed;
                    btn.Tag = "4";
                    if (Global._user_turn)
                        Global._user_turn = false;
                    else
                        Global._user_turn = true;
                    break;
                case "3":
                    //MessageBox.Show("Already been HIT");
                    break;
                case "4":
                    //MessageBox.Show("Already missed");
                    break;

            }
            return hitted_ok;
            //tblPanel_My.Controls.Remove(b);


        }

   //Gai
        void Board_MouseClick(object sender, MouseEventArgs e)
        {
            OnMouseClick(sender, e);
        }

        void Board_DragDrop(object sender, DragEventArgs e)
        {
            OnDragDrop(sender, e);
        }

        void Board_DragLeave(object sender, EventArgs e)
        {
            OnDragLeave(sender, e);
        }

        void Board_DragEnter(object sender, DragEventArgs e)
        {
            OnDragEnter(sender, e);
        }

        protected virtual void OnMouseClick(object sender, MouseEventArgs e)
        {
        }

        protected virtual void OnDragDrop(object sender, DragEventArgs e)
        {
        }

        protected virtual void OnDragLeave(object sender, EventArgs e)
        {
        }

        protected virtual void OnDragEnter(object sender, DragEventArgs e)
        {
        }
 

    }
}
